import axios from "axios";

let instance = axios.create({
  // baseURL: 'http://kumanxuan1.f3322.net:8881/cms',
  baseURL: "http://520it.com:8881/cms",
  timeout: 10000,
});

instance.interceptors.request.use(
  (config) => {
    // console.log("请求之前拦截...1");
    if (config.url === "/sendSMS") {
      // config.headers.Content-Type.
      config.headers["Content-Type"] = "application/x-www-form-urlencoded";
    }
    let mytoken = localStorage.getItem("x-auth-token");
    if (mytoken) {
      config.headers["x-auth-token"] = mytoken;
    }
    return config;
  },
  (err) => {
    return Promise.reject(err);
  }
);

instance.interceptors.response.use(
  (res) => {
    // console.log("响应之前拦截...2", res);
    if (res.data.code != 0 && res.data.code != 400 && res.data.code != 407) {
      alert(res.data.message);
      return false;
    }
    return res.data;
  },
  (err) => {
    return Promise.reject(err);
  }
);

export default instance;
